/**
 *  Copyright 2010 Kyle O'Connor, Justin Arling, Mike Mercieca
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package edu.drexel.sd0910.ece01.aqmon.weather;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * Makes an HTTP Request to the following URL: http://api.wunderground.com/auto
 * /wui/geo/GeoLookupXML/index.xml?query=lat,lon
 * 
 * @author Kyle O'Connor
 * 
 */
public class WeatherUndergroundHTTPRequest {

	private static final String QUERY_URL = "http://api.wunderground.com/auto/wui/geo/GeoLookupXML/index.xml?query=";
	private static final String COMMA = ",";

	/**
	 * Default constructor.
	 */
	public WeatherUndergroundHTTPRequest() {
		super();
	}

	public static InputStream get(String latStr, String lonStr) throws Exception {
		StringBuffer buf = new StringBuffer();
		buf.append(QUERY_URL);
		buf.append(latStr);
		buf.append(COMMA);
		buf.append(lonStr);
		String urlStr = buf.toString();
		return readFrom(urlStr, null);
	}

	public static InputStream get(String urlStr) throws Exception {
		return readFrom(urlStr, null);
	}

	public static InputStream post(String urlStr, String post) throws Exception {
		return readFrom(urlStr, post);
	}

	private static InputStream readFrom(String urlStr, String postStr) throws MalformedURLException, IOException {
		URLConnection conn = new URL(urlStr).openConnection();
		conn.setDoInput(true);
		if (postStr != null && postStr.length() > 0) {
			conn.setDoOutput(true);
			DataOutputStream output = new DataOutputStream(conn.getOutputStream());
			output.writeBytes(postStr);
			output.flush();
			output.close();
		}
		return conn.getInputStream();
	}

}
